package com.quanquan.user.controller.statisticsReport;

import base.BaseController;
import com.quanquan.user.configBeans.TimeTransformation;
import com.quanquan.user.entity.IntegralTradingDetails;
import com.quanquan.user.request.StatisticsReportRequestBody;
import com.quanquan.user.service.statisticsReport.IntegralStatisticsReportService;
import com.quanquan.user.vo.IntegralStatisticsReportVO;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.CalendarUtil;
import utils.DateUtils;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 功能描述:积分统计报表
 *
 * @ClassName: IntegralStatisticsReportController
 * @Author: xiaoqianh丶
 * @Date: 2019-05-27 17:22
 * @Version: V1.0
 */
@RestController
@RequestMapping("/statistical_integral")
public class IntegralStatisticsReportController extends BaseController {

    public static final String DAILY = "daily";
    public static final String WEEK = "week";
    public static final String MONTH = "month";
    public static final String YEAR = "year";

    @Autowired
    private IntegralStatisticsReportService integralStatisticsReportService;
    /**
     * 用户积分每日获得总量,用户积分每月获得总量,用户积分每月获得总量
     *
     * @return
     */
    @PostMapping("/integral_receive")
    public ResultBody integralReceive(@RequestBody StatisticsReportRequestBody requestBody) {
        String statisticalCycle = requestBody.getStatisticalCycle();
        Date startTime = null;
        Date endTime = null;
        Map<String, Date> map = new HashMap<>();
        if (StringUtil.isNotEmpty(statisticalCycle)) {
            switch (statisticalCycle) {
                case DAILY:
                    //近7天的当前时间
                    String afterDayDate = CalendarUtil.getAfterDayDate("-6");
                    //7天前的当天开始时间和结束时间
                    Map<String, Date> before = CalendarUtil.setDayRange(afterDayDate);
                    //当前时间的开始结束时间
                    Map<String, Date> after = CalendarUtil.setDayRange(map);
                    //真正的时间段
                    startTime = before.get("startTime");
                    endTime = after.get("stopTime");
                    break;
                case WEEK:
                    //当前时间的本周开始时间和结束时间
                    Map<String, Date> afterWeek = CalendarUtil.setWeekRange(map);
                    //本周开始时间 往前6周 前42天（近7周的开始时间）
                    Date countTime = afterWeek.get("startTime");
                    //近7周的开始时间
                    startTime = CalendarUtil.getAfterDayDate("-42", countTime);
                    endTime = afterWeek.get("stopTime");
                    break;
                case MONTH:
                    //当前时间的本月开始时间和结束时间
                    Map<String, Date> afterMonthDate = CalendarUtil.setMonthRange(map);
                    //本月开始时间 往前6月
                    Date countTime2 = afterMonthDate.get("startTime");
                    //前第6个月的开始时间
                    startTime = CalendarUtil.getAfterMonthDate("-6", countTime2);
                    endTime = afterMonthDate.get("stopTime");
                    break;
                case YEAR:
                    Map<String, Date> yearTime = CalendarUtil.setYearRange(map);
                    startTime = yearTime.get("startTime");
                    endTime = yearTime.get("stopTime");
                    Integer integer = integralStatisticsReportService.statisticalReportsReceiveYear(startTime, endTime);
                    String s = DateUtils.currentDate();
                    String time = s.substring(0,4);
                    Map yearMap = new HashMap();
                    yearMap.put(time, integer);
                    return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),yearMap);
                default:
                    throw new IllegalStateException("Unexpected value（参数错误）: " + statisticalCycle);
            }
        }

        List<IntegralTradingDetails> integralTradingDetails = integralStatisticsReportService.statisticalReportsReceive(startTime, endTime);
        Stream<IntegralTradingDetails> userStream = null;
        Map<String, Integer> result = new HashMap<>();
        Map<String, Integer> stringLongMap = new HashMap<>();
        //计算结果
        // （每日 按上午下午划分）
        if (DAILY.equals(statisticalCycle)) {
            integralTradingDetails.parallelStream().forEach(f ->f.setCreateTime(UserStatisticsReportController.handleTime(f.getCreateTime())));
        }
        // （每周按星期划分）
        if(WEEK.equals(statisticalCycle)){
            integralTradingDetails.parallelStream().forEach(f ->f.setCreateTime(UserStatisticsReportController.handleTimeWeek(f.getCreateTime())));
        }
        // （每月按每周划分）
        if(MONTH.equals(statisticalCycle)){
            integralTradingDetails.parallelStream().forEach(f ->f.setCreateTime(UserStatisticsReportController.handleTimeMonth(f.getCreateTime())));

        }
        //将积分明细实体映射到VO集合上
        List<IntegralStatisticsReportVO> integralTradingDetailsVOS = integralTradingDetails.parallelStream().map(e -> new IntegralStatisticsReportVO(
                e.getCreateTime(),
                Integer.parseInt(e.getIntegralPoint()))).
                collect(Collectors.toList());
        //  (每年按每月划分)
        result = integralTradingDetailsVOS.parallelStream().collect(Collectors.groupingBy(IntegralStatisticsReportVO :: getCreateTime, Collectors.summingInt(IntegralStatisticsReportVO ::getIntegralPoint)));


        if(DAILY.equals(statisticalCycle)){
            stringLongMap = TimeTransformation.formatDay3(result);
        }
        if(WEEK.equals(statisticalCycle)){
            stringLongMap = TimeTransformation.formatWeek3(result);
        }
        if(MONTH.equals(statisticalCycle)){
            stringLongMap = TimeTransformation.formatMonth3(result);
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),stringLongMap);
    }




    /**
     * 用户积分每日消费总量,用户积分每月消费总量,用户积分每月消费总量
     *
     * @return
     */
    @PostMapping("/integral_consume")
    public ResultBody integralConsume(@RequestBody StatisticsReportRequestBody requestBody) {
        String statisticalCycle = requestBody.getStatisticalCycle();
        Date startTime = null;
        Date endTime = null;
        Map<String, Date> map = new HashMap<>();
        if (StringUtil.isNotEmpty(statisticalCycle)) {
            switch (statisticalCycle) {
                case DAILY:
                    //近7天的当前时间
                    String afterDayDate = CalendarUtil.getAfterDayDate("-7");
                    //7天前的当天开始时间和结束时间
                    Map<String, Date> before = CalendarUtil.setDayRange(afterDayDate);
                    //当前时间的开始结束时间
                    Map<String, Date> after = CalendarUtil.setDayRange(map);
                    //真正的时间段
                    startTime = before.get("startTime");
                    endTime = after.get("stopTime");
                    break;
                case WEEK:
                    //当前时间的本周开始时间和结束时间
                    Map<String, Date> afterWeek = CalendarUtil.setWeekRange(map);
                    //本周开始时间 往前6周 前42天（近7周的开始时间）
                    Date countTime = afterWeek.get("startTime");
                    //近7周的开始时间
                    startTime = CalendarUtil.getAfterDayDate("-42", countTime);
                    endTime = afterWeek.get("stopTime");
                    break;
                case MONTH:
                    //当前时间的本月开始时间和结束时间
                    Map<String, Date> afterMonthDate = CalendarUtil.setMonthRange(map);
                    //本月开始时间 往前6月
                    Date countTime2 = afterMonthDate.get("startTime");
                    //前第6个月的开始时间
                    startTime = CalendarUtil.getAfterMonthDate("-6", countTime2);
                    endTime = afterMonthDate.get("stopTime");
                    break;
                case YEAR:
                    Map<String, Date> yearTime = CalendarUtil.setYearRange(map);
                    startTime = yearTime.get("startTime");
                    endTime = yearTime.get("stopTime");
                    Integer integer = integralStatisticsReportService.statisticalReportsConsumeYear(startTime, endTime);
                    String s = DateUtils.currentDate();
                    String time = s.substring(0,4);
                    Map yearMap = new HashMap();
                    yearMap.put(time, integer);
                    return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),yearMap);
                default:
                    throw new IllegalStateException("Unexpected value（参数错误）: " + statisticalCycle);
            }
        }

        List<IntegralTradingDetails> integralTradingDetails = integralStatisticsReportService.statisticalReportsConsume(startTime, endTime);
        Stream<IntegralTradingDetails> userStream = null;
        Map<String, Integer> result = new HashMap<>();
        Map<String, Integer> stringLongMap = new HashMap<>();
        //计算结果
        // （每日 按上午下午划分）
        if (DAILY.equals(statisticalCycle)) {
            integralTradingDetails.parallelStream().forEach(f ->f.setCreateTime(UserStatisticsReportController.handleTime(f.getCreateTime())));


        }
        // （每周按星期划分）
        if(WEEK.equals(statisticalCycle)){
            integralTradingDetails.parallelStream().forEach(f ->f.setCreateTime(UserStatisticsReportController.handleTimeWeek(f.getCreateTime())));
        }
        // （每月按每周划分）
        if(MONTH.equals(statisticalCycle)){
            integralTradingDetails.parallelStream().forEach(f ->f.setCreateTime(UserStatisticsReportController.handleTimeMonth(f.getCreateTime())));

        }
        //将积分明细实体映射到VO集合上
        List<IntegralStatisticsReportVO> integralTradingDetailsVOS = integralTradingDetails.parallelStream().map(e -> new IntegralStatisticsReportVO(
                e.getCreateTime(),
                Integer.parseInt(e.getIntegralPoint()))).
                collect(Collectors.toList());
        //  (每年按每月划分)
        result = integralTradingDetailsVOS.parallelStream().collect(Collectors.groupingBy(IntegralStatisticsReportVO :: getCreateTime, Collectors.summingInt(IntegralStatisticsReportVO ::getIntegralPoint)));

        if(DAILY.equals(statisticalCycle)){
            stringLongMap = TimeTransformation.formatDay3(result);
        }
        if(WEEK.equals(statisticalCycle)){
            stringLongMap = TimeTransformation.formatWeek3(result);
        }
        if(MONTH.equals(statisticalCycle)){
            stringLongMap = TimeTransformation.formatMonth3(result);
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),stringLongMap);
    }


}
